package com.hongxin.doc.common.response;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.hongxin.doc.common.enums.ResultCodeEnum;

import java.io.Serializable;

public class Result<T> implements Serializable {

    private Integer code;

    private String message;

    private boolean success;

    private T data;

    @JsonIgnore
    private ResultCodeEnum resultCode;


    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public ResultCodeEnum getResultCode() {
        return resultCode;
    }

    public void setResultCode(ResultCodeEnum resultCode) {
        this.resultCode = resultCode;
        this.setCode(resultCode.getCode());
        this.setMessage(resultCode.getMessage());
    }

    private Result() {}

    public static <T> Result<T> success() {
        Result<T> result = new Result<>();
        result.setResultCode(ResultCodeEnum.SUCCESS);
        result.setSuccess(true);
        return result;
    }

    public static <T> Result<T> success(String message) {
        Result<T> result = new Result<>();
        result.setCode(ResultCodeEnum.SUCCESS.getCode());
        result.setMessage(message);
        result.setSuccess(true);
        return result;
    }

    public static <T> Result<T> success(Integer code, String message) {
        Result<T> result = new Result<>();
        result.setCode(code);
        result.setMessage(message);
        result.setSuccess(true);
        return result;
    }

    public static <T> Result<T> success(T data) {
        Result<T> result = new Result<>();
        result.setResultCode(ResultCodeEnum.SUCCESS);
        result.setData(data);
        result.setSuccess(true);
        return result;
    }

    public static Result<String> error() {
        Result<String> result = new Result<>();
        result.setCode(ResultCodeEnum.INTERNAL_ERROR.getCode());
        result.setMessage(ResultCodeEnum.INTERNAL_ERROR.getMessage());
        return result;
    }

    public static Result<String> error(String message) {
        Result<String> result = new Result<>();
        result.setCode(ResultCodeEnum.INTERNAL_ERROR.getCode());
        result.setMessage(message);
        return result;
    }

    public static Result<String> error(Integer code, String message) {
        Result<String> result = new Result<>();
        result.setCode(code);
        result.setMessage(message);
        return result;
    }
}
